﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ART.BLL;
using ART.Common;

namespace ART.Admini.Users
{
    public partial class UserEdit : System.Web.UI.Page
    {
        protected int Id = 0;
        private ART.BLL.B_UsersDetail bll = new ART.BLL.B_UsersDetail();
        private ART.Model.M_UsersDetail model = new ART.Model.M_UsersDetail();
        private BLL.B_dictionary bll_dic = new BLL.B_dictionary();

        protected void Page_Load(object sender, EventArgs e)
        {
            Id = Common.LYRequest.GetInt("id", 0);
#warning BLL.B_TBAdmin.IsPermissions(0x6c)

            if (!IsPostBack)
            {
                InitData();
            }
        }
        private void InitData()
        {
            JBind.BindList(dropAreaId, bll_dic.GetList("parentid=3").Tables[0], "DictionaryName", "id");
            dropAreaId.Items.Insert(0, new ListItem("请选择", "0"));
            JBind.BindList(dropAreadIdY, bll_dic.GetList("parentid=3").Tables[0], "DictionaryName", "id");
            dropAreadIdY.Items.Insert(0, new ListItem("请选择", "0"));
            JBind.BindList(dropEducation, bll_dic.GetList("parentid=10").Tables[0], "DictionaryName", "id");
            dropEducation.Items.Insert(0, new ListItem("请选择", "0"));
            JBind.BindList(dropLanguage, bll_dic.GetList("parentid=11").Tables[0], "DictionaryName", "id");
            dropLanguage.Items.Insert(0, new ListItem("请选择", "0"));
            JBind.BindList(dropComputer, bll_dic.GetList("parentid=37").Tables[0], "DictionaryName", "id");
            dropComputer.Items.Insert(0, new ListItem("请选择", "0"));
            JBind.BindList(dropIDType, bll_dic.GetList("parentid=6").Tables[0], "DictionaryName", "id");
            dropIDType.Items.Insert(0, new ListItem("请选择", "0"));
            JBind.BindList(dropBirthplace, bll_dic.GetList("parentid=5").Tables[0], "DictionaryName", "DictionaryName");
            dropBirthplace.Items.Insert(0, new ListItem("请选择", "0"));
            JBind.BindList(dropEducation, bll_dic.GetList("parentid=10").Tables[0], "DictionaryName", "id");
            dropEducation.Items.Insert(0, new ListItem("请选择", "0"));
            JBind.BindList(dropWorkYear, bll_dic.GetList("parentid=7").Tables[0], "DictionaryName", "id");
            dropWorkYear.Items.Insert(0, new ListItem("请选择", "0"));
            JBind.BindList(dropManagerYear, bll_dic.GetList("parentid=8").Tables[0], "DictionaryName", "id");
            dropManagerYear.Items.Insert(0, new ListItem("请选择", "0"));
            JBind.BindList(dropCarYear, bll_dic.GetList("parentid=8").Tables[0], "DictionaryName", "id");
            dropCarYear.Items.Insert(0, new ListItem("请选择", "0"));


            if (Id > 0)
            {//edit
                model = bll.GetModel(Id);
                if (model != null)
                {
                    txtName.Text = model.Name;
                    rbltSex.SelectedValue = model.Sex;
                    imgPhoto.ImageUrl = model.Img;
                    txtBrithDay.Text = model.BirthDate.ToString("yyyy-MM-dd");
                    dropState.SelectedValue = model.State.ToString();
                    txtPhone.Text = model.Phone;
                    txtMobile.Text = model.Mobile;
                    txtAddress.Text = model.Address;
                    if (model.AreaId != 0)
                    {
                        Model.M_dictionary model_DIC = bll_dic.GetModel(model.AreaId);
                        if (model_DIC.ParentId == 0) //一级
                        {
                            this.dropAreaId.SelectedValue = model.AreaId.ToString();
                        }
                        else if (bll_dic.GetModel(model_DIC.ParentId).ParentId == 0) //二级
                        {
                            JBind.BindList(dropAreaId2, bll_dic.GetList("parentid=" + model_DIC.ParentId).Tables[0], "DictionaryName", "id");
                            dropAreaId2.Items.Insert(0, new ListItem("请选择市", "0"));
                            this.dropAreaId2.SelectedValue = model_DIC.Id.ToString();
                            this.dropAreaId.SelectedValue = model_DIC.ParentId.ToString();
                        }
                        else //三级
                        {
                            int parentid = bll_dic.GetModel(model_DIC.ParentId).ParentId;
                            model_DIC = bll_dic.GetModel(parentid);//sanji

                            JBind.BindList(dropAreaId2, bll_dic.GetList("parentid=" + parentid).Tables[0], "DictionaryName", "id");
                            dropAreaId2.Items.Insert(0, new ListItem("请选择市", "0"));

                            JBind.BindList(dropAreaId3, bll_dic.GetList("parentid=" + model_DIC.ParentId).Tables[0], "DictionaryName", "id");
                            dropAreaId3.Items.Insert(0, new ListItem("请选择区", "0"));


                            this.dropAreaId2.SelectedValue = parentid.ToString();
                            this.dropAreaId.SelectedValue = bll_dic.GetModel(parentid).ParentId.ToString();
                            this.dropAreaId3.SelectedValue = model.AreaId.ToString();
                        }
                    }


                    dropIDType.SelectedValue = model.IDType.ToString();
                    txtIDNumber.Text = model.IDNumber;
                    //txtIDAddress.Text = model.IDAddress;
                    if (model.IDAddress != "0")
                    {
                        Model.M_dictionary modelDic = bll_dic.GetModel(Utils.StrToInt(model.IDAddress, 0));
                        if (modelDic.ParentId == 0) //一级
                        {
                            this.dropAreadIdY.SelectedValue = model.IDAddress;
                        }
                        else if (bll_dic.GetModel(modelDic.ParentId).ParentId == 0) //二级
                        {
                            JBind.BindList(dropAreadIdY2, bll_dic.GetList("parentid=" + modelDic.ParentId).Tables[0], "DictionaryName", "id");
                            dropAreadIdY2.Items.Insert(0, new ListItem("请选择市", "0"));
                            this.dropAreadIdY2.SelectedValue = modelDic.Id.ToString();
                            this.dropAreadIdY.SelectedValue = modelDic.ParentId.ToString();
                        }
                    }

                    txtEmail.Text = model.Email;
                    txtQQ.Text = model.QQ;
                    txtEntryDate.Text = model.EntryDate.ToString("yyyy-MM-dd");
                    txtLeavadDate.Text = model.LeaveDate.ToString("yyyy-MM-dd");
                    txtRemarks.Text = model.Remarks;
                    txtNation.Text = model.Nation;
                    dropBirthplace.SelectedValue = model.Birthplace;
                    if (dropBirthplace.Items.FindByValue(model.Birthplace) == null)
                    {
                        txtBirthplace.Text = model.Birthplace;
                    }
                    txtProfessional.Text = model.Professional;
                    rbltMarriage.SelectedValue = model.Marriage;
                    txtHeight.Text = model.Height.ToString();
                    dropEducation.SelectedValue = model.Education.ToString();
                    txtLangScore.Text = model.LangScore.ToString();

                    dropLanguage.SelectedValue = model.Language.ToString();
                    dropComputer.SelectedValue = model.Computer.ToString();
                    dropWorkYear.SelectedValue = model.WorkYear.ToString();
                    dropManagerYear.SelectedValue = model.ManagerYear.ToString();
                    chkHavAbroad.Checked = model.HavAbroad;
                    chkHavCarSale.Checked = model.HavCarSale;
                    txtCarBrand.Text = bll_dic.GetString(model.CarBrand);
                    hidCarBrandId.Value = model.CarBrand;
                    dropCarYear.SelectedValue = model.CarYear.ToString();
                    chkHavCarManager.Checked = model.HavCarManager;
                    txtRelations.Text = model.Relations;
                    txtSkills.Text = model.Skills;
                    txtCardNumber.Text = model.CardNumber;
                    txtNotes.Text = model.Notes;
                    txtAchievement.Text = model.Achievement;
                    
                }
            }
        }
        protected void dropAreaId_SelectedIndexChanged(object sender, EventArgs p1)
        {
            if (this.dropAreaId.SelectedValue != "0")
            {
                dropAreaId2.Items.Clear();
                JBind.BindList(dropAreaId2, bll_dic.GetList("parentid=" + this.dropAreaId.SelectedValue).Tables[0], "DictionaryName", "id");
                dropAreaId2.Items.Insert(0, new ListItem("请选择", "0"));
            }
        }

        protected void dropAreaId2_SelectedIndexChanged(object sender, EventArgs p1)
        {
            if (this.dropAreaId2.SelectedValue != "0")
            {
                dropAreaId3.Items.Clear();
                JBind.BindList(dropAreaId3, bll_dic.GetList("parentid=" + this.dropAreaId2.SelectedValue).Tables[0], "DictionaryName", "id");
                dropAreaId3.Items.Insert(0, new ListItem("请选择", "0"));
            }
        }
        protected void dropAreadIdY_SelectedIndexChanged(object sender, EventArgs p1)
        {
            if (this.dropAreadIdY.SelectedValue != "0")
            {
                dropAreadIdY2.Items.Clear();
                JBind.BindList(dropAreadIdY2, bll_dic.GetList("parentid=" + this.dropAreadIdY.SelectedValue).Tables[0], "DictionaryName", "id");
                dropAreadIdY2.Items.Insert(0, new ListItem("请选择", "0"));
            }
        }
        protected void btnSave_Click(object sender, EventArgs p1)
        {
            if (bll.isExitUser(Id))
            {
                model = bll.GetModel(Id);
            }
            model.UId = Id;
            model.Name = txtName.Text;
            model.Sex = rbltSex.SelectedValue;
            if (this.FileUpload1.HasFile)
            {
                JUpload up = new JUpload();
                up.FileSize = 1000;
                up.FileType = "image";
                string rev = up.SaveFile(FileUpload1, "/UpLoads/User/");

                string strMsg = "";
                switch (rev)
                {
                    case "0":
                        strMsg = "上传文件格式不正确";
                        break;
                    case "1":
                        strMsg = "上传文件过大";
                        break;
                    case "3":
                        strMsg = "非法的图片";
                        break;

                }
                if (strMsg != "")
                {
                    MessageBox.Alert(sender, strMsg);
                    return;
                }

                model.Img = rev;
            }
            if (!string.IsNullOrEmpty(this.txtBrithDay.Text.Trim()))
            {
                model.BirthDate = DateTime.Parse(this.txtBrithDay.Text.Trim());
            }
            else
            {
                model.BirthDate = DateTime.Parse("1899-1-1 00:00:00");
            }
            model.State = dropState.SelectedValue;
            model.Birthplace = txtBirthplace.Text;
            model.Phone = txtPhone.Text;
            model.Mobile = txtMobile.Text;
            model.Address = txtAddress.Text;
            if (this.dropAreaId.SelectedValue != "0")
            {
                if (this.dropAreaId2.SelectedValue == "0")
                {
                    model.AreaId = int.Parse(this.dropAreaId.SelectedValue);
                }
                else if (this.dropAreaId3.SelectedValue == "0")
                {
                    model.AreaId = int.Parse(this.dropAreaId2.SelectedValue);
                }
                else
                {
                    model.AreaId = int.Parse(this.dropAreaId3.SelectedValue);
                }
            }

            model.IDType = int.Parse(dropIDType.SelectedValue);
            model.IDNumber = txtIDNumber.Text;
            if (this.dropAreadIdY2.SelectedValue == "0")
            {
                model.IDAddress = this.dropAreadIdY.SelectedValue;
            }
            else
            {
                model.IDAddress = this.dropAreadIdY2.SelectedValue;
            }
            model.Email = txtEmail.Text;
            model.QQ = txtQQ.Text;
            if (!string.IsNullOrEmpty(this.txtEntryDate.Text.Trim()))
            {
                model.EntryDate = DateTime.Parse(this.txtEntryDate.Text.Trim());
            }
            else
            {
                model.EntryDate = DateTime.Parse("1899-1-1 00:00:00");
            }
            if (!string.IsNullOrEmpty(this.txtLeavadDate.Text.Trim()))
            {
                model.LeaveDate = DateTime.Parse(this.txtLeavadDate.Text.Trim());
            }
            else
            {
                model.LeaveDate = DateTime.Parse("1899-1-1 00:00:00");
            }
            model.Remarks = txtRemarks.Text;
            if (dropBirthplace.SelectedValue == "0")
            {
                model.Birthplace = dropBirthplace.SelectedValue;
            }
            else
            {
                model.Birthplace = txtBirthplace.Text;
            }
            model.Professional = txtProfessional.Text;
            model.Nation = txtNation.Text;
            model.Marriage = rbltMarriage.SelectedValue;
            model.Height = decimal.Parse(txtHeight.Text);
            model.Education = int.Parse(dropEducation.SelectedValue);
            model.LangScore = int.Parse(txtLangScore.Text);
            model.Language = int.Parse(dropLanguage.SelectedValue);
            model.Computer = int.Parse(dropComputer.SelectedValue);
            model.WorkYear = int.Parse(dropWorkYear.SelectedValue);

            model.ManagerYear = int.Parse(dropManagerYear.SelectedValue);
            model.HavAbroad = chkHavAbroad.Checked;
            model.HavCarSale = chkHavCarSale.Checked;
            model.CarBrand = hidCarBrandId.Value;
            model.CarYear = int.Parse(dropCarYear.SelectedValue);
            model.HavCarManager = chkHavCarManager.Checked;
            model.Relations = txtRelations.Text;
            model.Skills = txtSkills.Text;
            model.CardNumber = txtCardNumber.Text;
            model.Notes = txtNotes.Text;
            //model.Age = int.Parse(txtAge.Text);
            model.Achievement = txtAchievement.Text;
            model.RefreshTime = DateTime.Now;

            if (bll.isExitUser(Id))
            {
                #region __________Edit__________
                if (bll.Update(model))
                {
                    B_operationLog.CreateOperationLog(B_operationLog.operationLogType.Edit, "用户", "编辑资料");
                    MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg(true);");
                }
                #endregion
            }
            else
            {
                
                bll.Add(model);
                //if (bll.Add(model) > 0)
                //{
                B_operationLog.CreateOperationLog(B_operationLog.operationLogType.Add, "用户", "编辑资料");
                MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg(true);");
                //}
            }
        }
    }
}
